<%-- 
    Document   : ProductForm
    Created on : 3 févr. 2010, 10:47:11
    Author     : JC
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="fr.uha.ensisa.kfetmanager.product.Product"%>
<%
            String action = request.getParameter("action");
            if (action == null || action.isEmpty()) {
                response.sendError(response.SC_BAD_REQUEST);
            }
            String name = "";
            String imageUrl = "";
            String price = "";
            Long id = Long.valueOf(-1);
            if (action.equals("edit")) {
                Product p;
                try {
                    p = (Product) getServletContext().getAttribute("product");
                } catch (Exception e) {
                    getServletContext().setAttribute("errorMessage", "Objet Product null");
                    getServletContext().setAttribute("redirectTo", "/admin/products");
                    getServletContext().getRequestDispatcher("/error.jsp").forward(request, response);
                    return;
                }
                id = p.getId();
                name = p.getName();
                imageUrl = p.getImageUrl();
                price = String.valueOf(p.getPrice());
            }
%>
<hr/>
<div>
    <h4><%=action.equals("edit") ? "Edition" : "Cr&eacute;ation"%></h4>
    <form method="POST" action="<%=getServletContext().getContextPath()%>/admin/products/<%=action%>">
        <%if (action.equals("edit")) {%>
        <input type="hidden" name="id" value="<%=id.toString()%>"/>
        <% }%>
        Nom: <input type="text" name="name" value="<%= name%>"/><br/>
        Prix: <input type="text" name="price" value="<%= price%>"/> €<br/>
        Adresse de l'image: <input type="text" name="imageUrl" value="<%= imageUrl%>"/><br/>
        <input type="submit" value="<%=action.equals("edit") ? "Sauvegarder" : "Cr&eacute;er produit"%>"/>
    </form>
    <% if (action.equals("edit")) {%>
    <p><a href="<%=getServletContext().getContextPath()%>/admin/products/delete?id=<%=id.toString()%>">Supprimer produit</a></p>
    <%}%>
</div>